package com.web.controller.cargo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.domain.cargo.*;
import com.github.pagehelper.PageInfo;
import com.service.cargo.ContractProductService;
import com.service.cargo.FactoryService;
import com.web.controller.BaseController;
import common.utils.PoiExcelUtil;
import common.utils.UploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/cargo/contractProduct")
//@DubboComponentScan
public class ContractProductController extends BaseController {

    @Reference
    private ContractProductService contractProductService;

    @Reference
    private FactoryService factoryService;


    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size, String contractId){
        ContractProductExample example = new ContractProductExample();
        ContractProductExample.Criteria criteria = example.createCriteria();
        criteria.andContractIdEqualTo(contractId);
        PageInfo pageInfo = contractProductService.findAll(example, page, size);
        request.setAttribute("page", pageInfo);

        //放入合同id
        request.setAttribute("contractId", contractId);

        //需要将生产货物的厂家放入
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        return "/cargo/product/product-list";
    }

    @RequestMapping("/edit")
    public String edit(ContractProduct contractProduct, MultipartFile productPhoto) throws IOException {
        contractProduct.setCompanyId(companyId);
        contractProduct.setCompanyName(companyName);

        //文件上传
        if (productPhoto != null) {
            String fileName = new UploadUtil().upload(productPhoto.getBytes());
            contractProduct.setProductImage(fileName);
        }

        request.setAttribute("contractId", contractProduct.getContractId());
        if (StringUtils.isBlank(contractProduct.getId())) {
            contractProductService.save(contractProduct);
        }else{
            contractProductService.update(contractProduct);
        }
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractProduct.getContractId();
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id){
        //需要将生产货物的厂家放入
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeEqualTo("货物");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList", factoryList);

        ContractProduct contractProduct = contractProductService.findById(id);
        request.setAttribute("contractProduct", contractProduct);
        return "/cargo/product/product-update";
    }

    @RequestMapping("/delete")
    public String delete(String id, String contractId){
        contractProductService.delete(id);
        request.setAttribute("contractId", contractId);
        return "redirect:/cargo/contractProduct/list.do?contractId=" + contractId;
    }

    @RequestMapping("/toImport")
    public String toImport(String contractId){
        request.setAttribute("contractId", contractId);
        return "/cargo/product/product-import";
    }

    @RequestMapping("/import")
    public String importExcel(String contractId, MultipartFile file) throws IOException {
        //获取文件流
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取页
        XSSFSheet sheet = workbook.getSheetAt(0);
        for (int i = 1; i < sheet.getLastRowNum(); i++) {
            //行
            XSSFRow row = sheet.getRow(i);

            Object[] objects = new Object[10];//对象属性有9个,设置长度为10,0索引不设置,取值时从1开始
            //从1索引开始
            for (int j = 1; j < 10; j++) {
                Cell cell = row.getCell(j);
                objects[j] = PoiExcelUtil.getCellValue(cell);
            }
            //通过一行的数据构造ContractProduct对象
            ContractProduct contractProduct = new ContractProduct(objects, companyId, companyName);
            contractProduct.setContractId(contractId);
            contractProductService.save(contractProduct);
        }
        request.setAttribute("contractId", contractId);
        return "redirect:/cargo/contractProduct/list.do?contractId="+contractId;
    }

}
